<?php

/**
* wemenuService.php
*
* 菜单Service层
*
* @author witiq <微信号：937731903>
* @version 1.0
* @package service
*/
require_once 'baseService.php';
class menuService extends baseService {

	/**
	 *@var 验证字段及规则
	 */
	protected $validate_rules = array(
							'men_parentId'=>array('is_number|4004&必须是整数'),	
						);

	protected $tree_fields = array(
								'id'=>'id',
								'parentid'=>'men_parentId',
								'name'    =>'men_name',
								'order'   =>'men_order'
								);

	public function __construct(){
		parent::__construct();
		$this->ownDao = InitPHP::getDao('menu');
	}


	/**
	 * [deleteModel 删除一个或多个数据]
	 * @param  [type] $ids [description]
	 * @return [type]      [description]
	 */
	public function deleteModel($ids){
		$ids = (array) $ids;	
		//如果存在子分类则不允许删除
		foreach ($ids as $id){
			$c = $this->ownDao->count(array('men_parentId'=>$id));
			if($c > 0){
				return array('error'=>'-1','msg'=>'存在子菜单，不能删除');	
			}			
		}
		return $this->ownDao->delete($ids);
	}

	/**
	 * [getMenu description]
	 * @return [type] [description]
	 */
	public function getMenu(){
		$res =  $this->ownDao->findList(array(),'','','',$this->tree_fields['order'],'ASC');
		$tree = $this->getLibrary('tree');
	    $tree->init($this->tree_fields);
		$temp = array();
		
		foreach($res as $row){
	
			if($row['men_parentId'] == 0 ){

				$childs = $tree->get_child($res, $row['id']);
				if(count($childs) > 0){
					$temp[] =array('men_name'=>$row['men_name'],'men_extend'=>$row['men_extend'],'childs'=>$childs);
				}else{
					$temp[] =array('men_name'=>$row['men_name'],'men_extend'=>$row['men_extend']);
				}

			}
		}
		return $temp;
	}

}